<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="dataForm.name"
          placeholder="名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="域" prop="domain">
        <el-input v-model="dataForm.domain" placeholder="域"></el-input>
      </el-form-item>
      <el-form-item   label="图片" prop="picture">
        <el-upload
           v-model="dataForm.picture"
          class="upload-demo"
          ref="upload"
          :http-request = "customUpload"
          drag
          action=""
          multiple
          >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">文件大小不超过50M</div>
        </el-upload>
      </el-form-item> 
     
      <el-form-item label="类型" prop="type">
        <el-input
          v-model="dataForm.type"
          placeholder="类型"
        ></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { fileUpload } from "@/utils";
//import QRCode from 'qrcode'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        domain: "",
        picture: [],
        type: ""
      },
      dataRule: {
       name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        domain: [{ required: true, message: "域不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    customUpload(picture) {
      fileUpload(picture).then(response => {
        this.dataForm.picture.push(response.data.url);
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/rencai/rencai/delete`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.supply.name;
              this.dataForm.domain = data.supply.domain;
              this.dataForm.type = data.supply.type;
              this.dataForm.picture = data.supply.picture;
              
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/rencai/rencai/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name:this.dataForm.name,
              domain:this.dataForm.domain,
              type:this.dataForm.type,
              picture:this.dataForm.picture.join(),
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",

                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
